package industry_2024.industry_04.extract

import org.apache.hudi.DataSourceWriteOptions.{PARTITIONPATH_FIELD, PRECOMBINE_FIELD, RECORDKEY_FIELD}
import org.apache.hudi.QuickstartUtils.getQuickstartWriteConfigs
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.lit

import java.text.SimpleDateFormat
import java.util.{Calendar, Properties}

object extract_count {
  def main(args: Array[String]): Unit = {
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("数据抽取")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .config("spark.sql.legacy.avro.datetimeRebaseModeInWrite","LEGACY")
      .config("spark.sql.legacy.avro.datetimeRebaseModeInRead","LEGACY")
      .enableHiveSupport()
      .getOrCreate()

    val connect=new Properties()
    connect.setProperty("user","root")
    connect.setProperty("password","123456")
    connect.setProperty("driver","com.mysql.jdbc.Driver")

    val day:Calendar=Calendar.getInstance()
    day.add(Calendar.DATE,-1)
    val yesterday=new SimpleDateFormat("yyyyMMdd").format(day.getTime)

    // 创建抽取数据的方法
    def to_hudi(mysql_name:String,hudi_name:String,primarykey:String,precombinefield:String):Unit={
      val hudi_path=s"hdfs://192.168.40.110:9000/user/hive/warehouse/hudi_gy_ods04.db/${hudi_name}"

      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false",mysql_name,connect)
        .withColumn("etldate",lit(yesterday))
        .write.mode("append")
        .format("hudi")
        .options(getQuickstartWriteConfigs)
        .option(RECORDKEY_FIELD.key(),primarykey)
        .option(PRECOMBINE_FIELD.key(),precombinefield)
        .option(PARTITIONPATH_FIELD.key(),"etldate")
        .option("hoodie.table.name",hudi_name)
        .save(hudi_path)
    }

    def to_hudi02(mysql_name: String, hudi_name: String, primarykey: String, precombinefield: String): Unit = {
      val hudi_path = s"hdfs://192.168.40.110:9000/user/hive/warehouse/hudi_gy_ods04.db/${hudi_name}"

      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false", mysql_name, connect)
        .drop("ProducePrgCode")
        .withColumn("etldate", lit(yesterday))
        .write.mode("append")
        .format("hudi")
        .options(getQuickstartWriteConfigs)
        .option(RECORDKEY_FIELD.key(), primarykey)
        .option(PRECOMBINE_FIELD.key(), precombinefield)
        .option(PARTITIONPATH_FIELD.key(), "etldate")
        .option("hoodie.table.name", hudi_name)
        .save(hudi_path)
    }

    to_hudi("ChangeRecord","changerecord","ChangeID,ChangeMachineID","ChangeEndTime")
    to_hudi("BaseMachine","basemachine","BaseMachineID","MachineAddDate")
    to_hudi02("ProduceRecord","producerecord","ProduceRecordID,ProduceMachineID","ProduceCodeEndTime")
    to_hudi("MachineData","machinedata","MachineRecordID","MachineRecordDate")



    spark.close()
  }

}
